package com.sk.bilibili.model.activity;

import android.Manifest;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.sk.api.annotations.Autowired;
import com.sk.bilibili.R;
import com.sk.bilibili.bean.Member;
import com.sk.bilibili.callBack.RequestCallBack;
import com.sk.bilibili.mapper.AndroidMapper;
import com.sk.bilibili.mapper.MemberMapper;
import com.sk.bilibili.model.BaseModel;
import com.sk.bilibili.other.BiliBiliProperty;
import com.sk.bilibili.other.JsonMap;
import com.sk.bilibili.utils.DataUtils;
import com.sk.bilibili.utils.PreferenceUtils;
import com.sk.bilibili.utils.RequestUtils;
import com.sk.bilibili.view.activity.FlashActivity;

public class FlashModel extends BaseModel<FlashActivity> {

    @Autowired
    BiliBiliProperty property;
    @Autowired
    PreferenceUtils preferenceUtils;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    AndroidMapper androidMapper;
    private long startTime;

    public void getFlash() {
        startTime =System.currentTimeMillis();
        String url = preferenceUtils.getString("flashUrl");
        if(url!=null)view.setFlash(url);//先使用缓存
        RequestUtils.process(androidMapper.getFlash(), flash -> {
            Log.e("TEST",flash);
            preferenceUtils.setString("flashUrl",flash);//更改下次生效
            if(url==null)view.setFlash(flash);
        });
    }

    public void waitAndLogin() {//最多等  定义的flashTime 时间 若因为网络原因等了很长时间直接登录
        long flashTime = property.getInt("flashTime")-System.currentTimeMillis()+ startTime;
        if(flashTime>0){
            new Handler(Looper.getMainLooper()).postDelayed(this::login, flashTime);
        }else {
            login();
        }
    }

    private void login(){
        String phone = preferenceUtils.getString("phone");
        String password = preferenceUtils.getString("password");
        if(phone==null||password==null){
            view.loginError();
        }else {
            JsonMap map=new JsonMap();
            map.put("member.phone",phone);
            map.put("member.password",password);
            RequestUtils.process(memberMapper.login(map), new RequestCallBack<Member>() {
                @Override
                public void success(Member member) {
                    DataUtils.setMemberId(member.getId());
                    preferenceUtils.setString("bSessionId",member.getExtra().getBSessionId());
                    view.loginSuccess();
                }
                @Override
                public void error(Throwable throwable) {
                    view.loginError();
                }
            });
        }
    }

    public void requestPermissions() {
        view.requestPermissions(new String[]{Manifest.permission.INTERNET,
                Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE},2233);
    }

    public void initThem() {
        DataUtils.setTheme(preferenceUtils.getInt("theme", R.style.Theme_BiliBiliDay));
    }

}